package algorithm;

import java.util.Arrays;

/**
 * 合并两个有序数组 
 *
 * @author apple
 **/
public class MergeList {

    /**
     * 合并两个有序数组
     **/
    public static int[] mergeList(int[] a, int[] b) {
        int len1 = a.length;
        int len2 = b.length;
        int[] c = new int[len1 + len2];
        int i = 0, j = 0, k = 0;
        while (i < len1 && j < len2) {
            if (a[i] < b[j]) {
                /**a[i++] 表示先取a[i]的值，然后i++
                 * 等同于 c[k]=a[i];
                 *        k++;
                 *        i++;
                 * **/
                c[k++] = a[i++];
            } else {
                c[k++] = b[j++];
            }
        }
        while (i < len1) {
            c[k++] = a[i++];
        }
        while (j < len2) {
            c[k++] = b[j++];
        }
        return c;
    }

    public static void main(String[] args) {
        int[] a = {1, 3, 5};
        int[] b = {2, 4,4, 6, 8, 10};
        int[] c = mergeList(a, b);
        Arrays.stream(c).forEach((int x)->{System.out.println(x);});
    }
}
